#include "MainController.h"
#include <QPushButton>

MainController::MainController(QMainWindow * mw)
{
    MainApplicationWindow = mw;
    ConfigPanel = MainApplicationWindow->findChild<QFrame *>("configPanel");
    lblWorkingImage = MainApplicationWindow->findChild<QLabel *>("lblWorkingImage");
    lblPreviewImage = MainApplicationWindow->findChild<QLabel *>("lblPreviewImage");
    MainImageManager = new ImageManager();
    MainImageManager->setImportSize(QSize(500, 500));
    CurrentImageProcessorModule = NULL;
}

void MainController::setCurrentImageProcessorModule(ImageProcessorModule *module)
{
    clearConfigPanel();
    if (CurrentImageProcessorModule) {
        delete CurrentImageProcessorModule;
    }
    CurrentImageProcessorModule = module;
    CurrentImageProcessorModule->initialize();
    CurrentImageProcessorModule->populatePanel(ConfigPanel);

}

void MainController::displayWorkingImage()
{
    lblWorkingImage->setPixmap(*MainImageManager->getDisplayableWorkingImage());
}

void MainController::displayPreviewImage()
{
    lblPreviewImage->setPixmap(*MainImageManager->getDisplayablePreviewImage());
}

void MainController::clearConfigPanel()
{
    foreach(QObject * child, ConfigPanel->children()){
        delete child;
    }
}

QFrame * MainController::getConfigPanel()
{
    return ConfigPanel;
}

void MainController::on_actionOpen_triggered()
{
    QString sourceFileName = QFileDialog::getOpenFileName(
                                        MainApplicationWindow,
                                        QString("Open Image"),
                                        QString::null,
                                        QString("Image Files (*.png *.jpg *.jpeg *.bmp)"));
lblWorkingImage->clear();
lblPreviewImage->clear();
OriginalFileName = sourceFileName;
    MainImageManager->loadOriginalImageFromFile(sourceFileName);
    displayWorkingImage();
    displayPreviewImage();
}

void MainController::on_actionSave_triggered()
{
    qDebug() << "Save action triggered.";
    if (MainImageManager->isOriginalImageLoaded()){
        MainImageManager->exportWorkingImage(OriginalFileName, MainImageManager->getWorkingImage());
    }
}

void MainController::on_actionSaveAs_triggered()
{
    if (MainImageManager->isOriginalImageLoaded()) {
        QString saveFileName = QFileDialog::getSaveFileName(
                    MainApplicationWindow,
                    QString("Save Image"),
                    QString::null,
                    QString("Image Files (*.png *.jpg *.jpeg *.bmp)"));
        if (saveFileName.isNull() == false && saveFileName.isEmpty() == false) {
            MainImageManager->exportWorkingImage(saveFileName, MainImageManager->getWorkingImage());
        }
    }
    else{
        report_ImageNotLoaded();
    }
}

void MainController::on_actionQuit_triggered()
{
    MainApplicationWindow->close();
}

void MainController::on_actionAbout_triggered()
{
    QGroupBox *gbox = new QGroupBox("Authors");
    QLabel *l1 = new QLabel("Ionut Camil Bancioiu");
    QLabel *l2 = new QLabel("Alin Anton Bogdan");
    QLabel *l3 = new QLabel("Eduard Jarnea");
    QLabel *l4 = new QLabel("Alexandra-Lavinia Popescu");
    QVBoxLayout * layout = new QVBoxLayout;
    layout->addWidget(l1);
    layout->addWidget(l2);
    layout->addWidget(l3);
    layout->addWidget(l4);
    gbox->setLayout(layout);

    QLabel * purpose = new QLabel("Advanced Computer Graphics");

    QVBoxLayout * box = new  QVBoxLayout;

    box->addWidget(gbox);
    purpose->setAlignment(Qt::AlignHCenter);
    box->addWidget(purpose);

    QLabel * l5 = new QLabel("ACS Masters");
    l5->setAlignment(Qt::AlignHCenter);
    QLabel *l6 = new QLabel("1st Year, 2012");
    l6->setAlignment(Qt::AlignHCenter);
    box->addWidget(l5);
    box->addWidget(l6);
    QDialog * dialog = new QDialog(MainApplicationWindow);

    QPushButton *close = new QPushButton("Close");
    close->connect(close, SIGNAL(clicked()), dialog, SLOT(close()));

    box->addWidget(close);

    dialog->setWindowTitle("About");
    dialog->setLayout(box);
    dialog->show();

}

void MainController::on_btnApply_clicked()
{
    QImage * newImage = new QImage(CurrentImageProcessorModule->process(*(MainImageManager->getWorkingImage())));
    MainImageManager->setWorkingImage(newImage);
    MainImageManager->resetPreview();
    displayWorkingImage();
    displayPreviewImage();
}

void MainController::on_btnPreviewProcessor_clicked()
{
    QImage * newImage = new QImage(CurrentImageProcessorModule->process(*(MainImageManager->getWorkingImage())));
    MainImageManager->setPreviewImage(newImage);
    displayPreviewImage();
}

void MainController::on_actionExampleProcessor_triggered()
{
    setCurrentImageProcessorModule(new ExampleProcessor());
}

void MainController::on_actionNegative_triggered()
{
    setCurrentImageProcessorModule(new NegativeModule());
}

void MainController::on_actionBrightness_triggered()
{
    setCurrentImageProcessorModule(new BrightnessModule());
}

void MainController::on_actionFlip_triggered(){
    setCurrentImageProcessorModule(new FlipProcessor(MainImageManager->getDisplayablePreviewImage()->width(), MainImageManager->getDisplayablePreviewImage()->height()));
}

void MainController::on_actionGrayscale_triggered(){
    setCurrentImageProcessorModule(new BlackWhiteProcessor());
}

void MainController::on_btnReset_clicked()
{
    lblWorkingImage->clear();
    lblPreviewImage->clear();
    MainImageManager->reset();
    displayWorkingImage();
    displayPreviewImage();
}

void MainController::report_ImageNotLoaded()
{
    QMessageBox mBox;
    mBox.setWindowTitle("Error");
    mBox.setText("Please load an image first.");
    mBox.setStandardButtons(QMessageBox::Ok);
    mBox.exec();
}
